#!/bin/bash
#set -x
WORK_PATH=$(readlink -f ${0} | xargs dirname)
script_name=$(basename $0)
DATE=$(date +%Y%m%d)
start_time_s=$(date +%s)

#######################################################
# yocto build result test .
#######################################################
DOCKER_BUILD_PATH="/workdir"
DOCKER_HOME_PATH="/home/hosthome/bev"
DOCKER_RELEASE_PATH="${DOCKER_HOME_PATH}/release"
DOCKER_GITLAB_PATH="${DOCKER_HOME_PATH}/srv"
RUNTIME_DOCKER_PATH="${DOCKER_BUILD_PATH}/build-sim-cdc/tmp/deploy/images/genericx86-64"
SDK_DOCKER_PATH="${DOCKER_BUILD_PATH}/build-sim-cdc/tmp/deploy/sdk-genericx86-64"
CODE_DOCKER_PATH="${DOCKER_BUILD_PATH}/build-sim-cdc/tmp/work/core2-64-arene-linux"
HOME_PATH="/home/wovendev/bev"
HOME_RELEASE_PATH="${HOME_PATH}/release"
HOME_GITLAB_PATH="${HOME_PATH}/srv"
#######################################################
# update branch
#######################################################
SP="0.4.0.0-rc.1"
GITLAB_URL="https://gitlab.geniie.net/BEVS3CDC/"
# GITLAB_URL="http://10.1.50.219/bevs3cdc"
GITLAB_YOCTO_BRANCH="feature/yocto-build-bev"
# GITLAB_MKDOCS_BRANCH="feature/mkdocs-build"
# GITLAB_DRAFT_BRANCH="feature/mkdocs-build-draft"
REPO_RECIPES=(
    # repo_name:repo_bb_file_pwth:bb_file_nanme:tag:tag
    #  code:dn-bevs3-cdc:meta-dn-cdc
    #  code:dn-bevs3-cdc:dn-cdc-hal-cec-impl-qc:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-cec-impl-qc:dn-cdc-hal-cec-impl-qc_1.0.0.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-input-impl-qc:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-input-impl-qc:dn-cdc-hal-input-impl-qc_1.0.0.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-input:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-input:dn-cdc-hal-input_git.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-usb-impl-qc:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-usb:dn-cdc-hal-usb-impl-qc_1.0.0.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-usb:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-usb:dn-cdc-hal-usb_1.0.0.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-lansrv:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-lansrv:dn-cdc-hal-lansrv_git.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-lansrv-impl-qc:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-lansrv-impl-qc:dn-cdc-hal-lansrv-impl-qc_1.0.0.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-most:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-most:dn-cdc-hal-most_git.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-most-impl-qc:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-most-impl-qc:dn-cdc-hal-most-impl-qc_1.0.0.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-avclan:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-avclan:dn-cdc-hal-avclan_git.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-avclan-impl-qc:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-avclan-impl-qc:dn-cdc-hal-avclan-impl-qc_1.0.0.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-backup-impl-qc:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-backup-impl-qc:dn-cdc-hal-backup-impl-qc_1.0.0.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-hal-stub-common:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-stub-common:dn-cdc-hal-stub-common_1.0.0.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-systemd-collection:meta-dn-cdc/meta-dn-cdc/recipes-core/dn-cdc-systemd-collection:dn-cdc-systemd-collection_git.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-appmng-pf-system-append-common:meta-dn-cdc/meta-dn-cdc/recipes-system/dn-cdc-appmng-pf-system-append-common:dn-cdc-appmng-pf-system-append-common.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-appmng-manifest-collection:meta-dn-cdc/meta-dn-cdc/recipes-tools/dn-cdc-appmng-manifest-collection:dn-cdc-appmng-manifest-collection.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-appmng-pf-system-append-qcom:meta-dn-cdc/meta-dn-cdc/recipes-system/dn-cdc-appmng-pf-system-append-qcom:dn-cdc-appmng-pf-system-append-qcom.bb:SRC_URI:SRCREV
    #  code:dn-bevs3-cdc:dn-cdc-appmng-pf-system-data-qcom:meta-dn-cdc/meta-dn-cdc/recipes-system/dn-cdc-appmng-pf-system-data-qcom:dn-cdc-appmng-pf-system-data-qcom.bb:SRC_URI:SRCREV


    # code:dn-bevs3-cdc:dn-cdc-hal-power:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-power:dn-cdc-hal-power_git.bb:SRC_URI:SRCREV
    # code:dn-bevs3-cdc:dn-cdc-hal-power-impl-qc:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-power-impl-qc:dn-cdc-hal-power-impl-qc_1.0.0.bb:SRC_URI:SRCREV
    # code:dn-bevs3-cdc:dn-cdc-hal-cinemoex:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-cinemoex:dn-cdc-hal-cinemoex_1.0.0.bb:SRC_URI:SRCREV
    # code:dn-bevs3-cdc:dn-cdc-hal-bluetooth:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-bluetooth:dn-cdc-hal-bluetooth_1.0.0.bb:SRC_URI:SRCREV
    # code:dn-bevs3-cdc:dn-cdc-hal-wifi:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-wifi:dn-cdc-hal-wifi_1.0.0.bb:SRC_URI:SRCREV
    # code:dn-bevs3-cdc:dn-cdc-hal-vehicle-impl-qc:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-vehicle-impl-qc:dn-cdc-hal-vehicle-impl-qc_1.0.0.bb:SRC_URI:SRCREV
    # code:dn-bevs3-cdc:dn-cdc-hal-vehicle:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-vehicle:dn-cdc-hal-vehicle_git.bb:SRC_URI:SRCREV
    # code:dn-bevs3-cdc:dn-cdc-hal-dtv-api:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-dtv:dn-cdc-hal-dtv-api_1.0.0.bb:SRC_URI:SRCREV
    # code:dn-bevs3-cdc:dn-cdc-hal-radio:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-radio:dn-cdc-hal-radio_1.0.0.bb:SRC_URI:SRCREV
    # code:dn-bevs3-cdc:dn-cdc-hal-sxm:meta-dn-cdc/meta-dn-cdc/recipes-hal/dn-cdc-hal-sxm:dn-cdc-hal-sxm_1.0.0.bb:SRC_URI:SRCREV
)

######################################################
# common function
######################################################
function start_run
{
    if [[ $(pwd) == ${DOCKER_BUILD_PATH} ]]; then
        if [[ -e ${DOCKER_HOME_PATH}/log ]]; then
            rm -rf ${DOCKER_HOME_PATH}/log/*
        else
            mkdir -p ${DOCKER_HOME_PATH}/log
        fi
        
        if [[ -e ${DOCKER_RELEASE_PATH}/yocto-build-${DATE} ]]; then
            echo "[INFO ] ${DOCKER_RELEASE_PATH}/yocto-build-${DATE} exists." | tee -a ${log}
        else
            mkdir -p ${DOCKER_RELEASE_PATH}/yocto-build-${DATE}
            mkdir -p ${DOCKER_RELEASE_PATH}/yocto-build-${DATE}-bak
            echo "[INFO ] mkdir ${DOCKER_RELEASE_PATH}/yocto-build-${DATE} Success." | tee -a ${log}
        fi
    fi

    if [[ $(pwd) == ${HOME_PATH}/tool ]]; then
        if [[ -e ${HOME_PATH}/log ]]; then
            rm -rf ${HOME_PATH}/log/*
        else
            mkdir -p ${HOME_PATH}/log
        fi

        if [[ -e ${HOME_RELEASE_PATH}/yocto-build-${DATE} ]]; then
            echo "[INFO ] ${HOME_RELEASE_PATH}/yocto-build-${DATE} exists." | tee -a ${log}
        else
            mkdir -p ${HOME_RELEASE_PATH}/yocto-build-${DATE}
            mkdir -p ${HOME_RELEASE_PATH}/yocto-build-${DATE}-bak
            echo "[INFO ] mkdir ${HOME_RELEASE_PATH}/yocto-build-${DATE} Success." | tee -a ${log}
        fi
    fi
    echo "#####################################################"  | tee -a ${log}
    echo "# Start Time     : $(date +%Y/%m/%d-%H:%M:%S)"          | tee -a ${log}
    echo "#####################################################"  | tee -a ${log}
    echo ""
}

function succ_exit
{
    local end_time_s=$(date +%s)
    echo "######################################################"  | tee -a ${log}
    echo "# Execution Result : Success "                           | tee -a ${log}
    echo "# Execution Time   : $[ $end_time_s - $start_time_s ]s"  | tee -a ${log}
    echo "# End Time         : $(date +%Y%m%d-%H:%M:%S)"           | tee -a ${log}
    echo "######################################################"  | tee -a ${log}
    exit 0
}

function abnormal_exit
{
    local end_time_s=$(date +%s)
    echo "######################################################"  | tee -a ${log}
    echo "# Execution Result : ERROR !!! "                         | tee -a ${log}
    echo "# Execution Time   : $[ $end_time_s - $start_time_s ]s"  | tee -a ${log}
    echo "# End Time         : $(date +%Y%m%d-%H:%M:%S)"           | tee -a ${log}   
    echo "######################################################"  | tee -a ${log}
    exit 1
}

function runtime_docker_backup
{
    if [[ $(pwd) != ${DOCKER_BUILD_PATH} ]]; then
        echo "The current execution directory is wrong !!!"
        exit 1
    fi
    echo ">>>>>>>>>>>>>>>>>>>>>>>>>> runtime_docker_backup start>>>>>>>>>>>>>>>>>>>>>>>>>>>" | tee -a ${log}
    local runtime_docker_filename=$(ls ${RUNTIME_DOCKER_PATH}/arene-image-cockpit-cdc-genericx86-64-*.rootfs.tar.bz2)
    if [[ ${runtime_docker_filename} ]]; then
        echo "[INFO ] === the new runtime_docker_filename = [${runtime_docker_filename}]" | tee -a ${log}
        cp ${runtime_docker_filename} ${DOCKER_RELEASE_PATH}/yocto-build-${DATE}/
        if [[ $? -ne 0 ]]; then     
            echo "[ERR ] ${RUNTIME_DOCKER_PATH}/${runtime_docker_filename} is not found !!!" | tee -a ${log}
            abnormal_exit
        else
            echo "[INFO ] [${RUNTIME_DOCKER_PATH}/${runtime_docker_filename}] back to [${DOCKER_RELEASE_PATH}/yocto-build-${DATE}] success" | tee -a ${log}
        fi
    fi
    echo "<<<<<<<<<<<<<<<<<<<<<<<< runtime_docker_backup end<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" | tee -a ${log}
    echo ""
}

function result_check
{
    local result_branch_info="${DOCKER_RELEASE_PATH}/yocto-build-${DATE}/${SP}_relult_branch_info_$(date +%Y%m%d%H%M%S).txt"
    local update_branch_info="${DOCKER_RELEASE_PATH}/yocto-build-${DATE}/${SP}_update_branch_info_*.txt"
    echo "---------------check repo info in the yocto (start)--------------" | tee -a ${result_branch_info}
    for repo_recipes in ${REPO_RECIPES[@]}; do 
        tmp_array=(${repo_recipes//:/ })
        recipes=${tmp_array[2]}
        if [[ $(ls ${CODE_DOCKER_PATH}/${recipes} |wc -l) -eq 1 ]];then
            pushd ${CODE_DOCKER_PATH}/${recipes}/*/git
            {
                echo "-------------- repo = ${recipes}---------------" | tee -a  ${result_branch_info}
                SRC_URI="SRC_URI = \"$(git remote get-url origin | sed 's/https:/git:/g');protocol=https;branch=$(git rev-parse --abbrev-ref HEAD)\""
                SRCREV="SRCREV = \"$(git rev-parse HEAD)\""
                # echo ${SRC_URI} | tee -a ${result_branch_info}
                # echo ${SRCREV} | tee -a  ${result_branch_info}
                tmp_src_url=$(git remote get-url origin | sed 's/https:/git:/g');protocol=https;branch=$(git rev-parse --abbrev-ref HEAD)
                if grep ${tmp_src_url} ${update_branch_info} > /dev/null ; then
                    echo "${SRC_URI} Check passed" | tee -a ${result_branch_info}
                else
                    echo "${SRC_URI} Check ERROR !!!" | tee -a ${result_branch_info}
                fi
                tmp_srcrev=$(git rev-parse HEAD)
                if grep ${tmp_srcrev} ${update_branch_info} > /dev/null ; then
                    echo "${SRCREV} Check passed" | tee -a ${result_branch_info}
                else
                    echo "${SRCREV} Check ERROR !!!" | tee -a ${result_branch_info}
                fi
                echo "" | tee -a  ${result_branch_info}
            }
            popd
        fi
    done
    echo "---------------check repo info in the yocto (end)--------------" | tee -a ${result_branch_info}
    echo "" | tee -a ${result_branch_info}
}

function revert_bb_file ()
{
    echo ">>>>>>>>>>>>>>>>>>>>>>>>>> revert_bb_file start >>>>>>>>>>>>>>>>>>>>>>>>>>>"
    for repo_info in ${REPO_RECIPES[@]}; do
        local tmp_date=(${repo_info//:/ })
        local group_name=${tmp_date[1]}
        local repo_name=${tmp_date[2]}
        local repo_path=${tmp_date[3]}
        local repo_bb_file=${tmp_date[4]}
        local repo_srcuri_name=${tmp_date[5]}
        local repo_srcrev_name=${tmp_date[6]}
        
        pushd ${DOCKER_BUILD_PATH}/${repo_path}
        { 
            local bb_srcurl=$(grep \#${repo_srcuri_name} ${repo_bb_file})
            if [[ ${bb_srcurl} ]]; then
                echo "[INFO ] #${repo_srcuri_name} is exist, revert"
                sed -ri "/^SRC_URI/d" ./${repo_bb_file}
                sed -ri "/^SRCREV/d" ./${repo_bb_file}
                sed -ri "s/^#${repo_srcuri_name}/${repo_srcuri_name}/g" ./${repo_bb_file}
                sed -ri "s/^#${repo_srcrev_name}/${repo_srcrev_name}/g" ./${repo_bb_file}
            fi
        }
        popd
    done
    echo ">>>>>>>>>>>>>>>>>>>>>>>>>> revert_bb_file end >>>>>>>>>>>>>>>>>>>>>>>>>>>"
}

function update_bb_file
{
    if [[ $(pwd) != ${DOCKER_BUILD_PATH} ]]; then
        echo "The current execution directory is wrong !!!"
        exit 1
    fi
    
    mv ${DOCKER_RELEASE_PATH}/yocto-build-${DATE}/* ${DOCKER_RELEASE_PATH}/yocto-build-${DATE}-bak
    if [[ $? -ne 0 ]]; then
        echo "[ERR ] Failed to mv [${DOCKER_RELEASE_PATH}/yocto-build-${DATE}/*] TO [${DOCKER_RELEASE_PATH}/yocto-build-${DATE}-bak]."
    else
        echo "[INFO ] mv [${DOCKER_RELEASE_PATH}/yocto-build-${DATE}/*] TO [${DOCKER_RELEASE_PATH}/yocto-build-${DATE}-bak] Succ."
    fi 

    local log="${DOCKER_HOME_PATH}/log/update_bb_file.log"
	local branch_info="${DOCKER_RELEASE_PATH}/yocto-build-${DATE}/${SP}_update_branch_info_$(date +%Y%m%d%H%M%S).txt"
    :> ${log}
	:> ${branch_info}
    echo ">>>>>>>>>>>>>>>>>>>>>>>>>> update_bb_file start >>>>>>>>>>>>>>>>>>>>>>>>>>>"
    for repo_info in ${REPO_RECIPES[@]}; do
        local tmp_date=(${repo_info//:/ })
        local group_name=${tmp_date[1]}
        local repo_name=${tmp_date[2]}
        local repo_path=${tmp_date[3]}
        local repo_bb_file=${tmp_date[4]}
        local repo_srcuri_name=${tmp_date[5]}
        local repo_srcrev_name=${tmp_date[6]}

        echo "[INFO ] >>>>>>>>>>> Get the [${repo_name}/${GITLAB_YOCTO_BRANCH}] branch information start " | tee -a ${log}
        pushd ${DOCKER_GITLAB_PATH} >> ${log}
        {   if [[ -e ${repo_name} ]]; then
                pushd ${repo_name} >> ${log} 2>&1
                    git reset --hard HEAD >/dev/null >> ${log} 2>&1
					git clean -df >/dev/null >> ${log} 2>&1
					git remote update origin --prune >/dev/null >> ${log} 2>&1
					git checkout master  >> ${log} 2>&1
					git pull >> ${log} 2>&1
					git checkout ${GITLAB_YOCTO_BRANCH}  >> ${log} 2>&1
            else
                git clone --progress "${GITLAB_URL}/${group_name}/${repo_name}.git" ${repo_name} > ${DOCKER_GITLAB_PATH}/.git_progress.log
				pushd ${repo_name} >>${log} 2>&1
				{
					git pull >> ${log} 2>&1
					git checkout ${GITLAB_YOCTO_BRANCH} >> ${log} 2>&1
                    if [[ $? -ne 0 ]]; then
                        echo "[ERROR] [git checkout ${GITLAB_YOCTO_BRANCH}] ERROR !!!" | tee -a  ${log}
                        echo "[INFO ] >>>>>>>>>>> Get the [${repo_name}/${GITLAB_YOCTO_BRANCH}] information ERROR !!! " | tee -a  ${log}
                        continue;
                    fi
				}
            fi
				git pull  >>${log} 2>&1
				repo_srcuri="$(git remote get-url origin  | sed  's/https:/git:/g');protocol=https;branch=${GITLAB_YOCTO_BRANCH}"
				repo_srcrev="$(git rev-parse HEAD)"
                echo "######### ${repo_name} #########" >> ${branch_info}
				echo "SRC_URI = \"${repo_srcuri}\"" | tee -a ${branch_info}
				echo "SRCREV = \"${repo_srcrev}\"" | tee -a ${branch_info}
                echo "" >> ${branch_info}
                git checkout master  >> ${log} 2>&1
                git branch -D ${GITLAB_MKDOCS_BRANCH}  >>${log} 2>&1
				popd >> ${log}
        }
        popd >> ${log}
        echo "[INFO ] >>>>>>>>>>> Get the [${repo_name}/${GITLAB_YOCTO_BRANCH}] information end " | tee -a  ${log}

        echo "[INFO ] >>>>>>>>>>> update [${DOCKER_BUILD_PATH}/${repo_path}/${repo_bb_file}] start " | tee -a  ${log}
        pushd ${DOCKER_BUILD_PATH}/${repo_path}  >>${log} 2>&1
        { 
            local bb_srcurl=$(grep \#${repo_srcuri_name} ${repo_bb_file})
            if [[ ! ${bb_srcurl} ]]; then
                echo "[INFO ] #${repo_srcuri_name} does not exist, first"
                sed -ri "s/^${repo_srcuri_name}/#${repo_srcuri_name}/g" ./${repo_bb_file}
                sed -ri "/^#${repo_srcuri_name}.*/a\SRC_URI = \"${repo_srcuri}\"" ./${repo_bb_file}

                sed -ri "s/^${repo_srcrev_name}/#${repo_srcrev_name}/g" ./${repo_bb_file}
                sed -ri "/^#${repo_srcrev_name}.*/a\SRCREV = \"${repo_srcrev}\"" ./${repo_bb_file}
            else
                echo "[INFO ] #${repo_srcuri_name} is exist, second"
                sed -ri "/^SRC_URI/d" ./${repo_bb_file}
                sed -ri "/^SRCREV/d" ./${repo_bb_file}
                
                sed -ri "/^#${repo_srcuri_name}.*/a\SRC_URI = \"${repo_srcuri}\"" ./${repo_bb_file}
                sed -ri "/^#${repo_srcrev_name}.*/a\SRCREV = \"${repo_srcrev}\"" ./${repo_bb_file}
            fi
            echo "[INFO ] <<<<<<<<<<< update [${HOME_REPO_PATH}/${repo_path}/${repo_bb_file}] end " | tee -a  ${log}
            echo ""
        }
        popd  >>${log}
    done
    echo "<<<<<<<<<<<<<<<<<<<<<<<< update_bb_file end <<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
    echo ""
}

function bitbake_cleansstate ()
{
    if [[ $(pwd) != ${DOCKER_BUILD_PATH} ]]; then
        echo "The current execution directory is wrong !!!"
        exit 1
    fi
    echo "Please enter the type of environment variable to load:"
    echo "0 : source build-sim-cdc/setup-build-env"
    echo "1 : source build-qcom-adp/setup-build-env"
    echo "2 : source build-qcom-cdc/setup-build-env"
    read -p "Please enter your options:" options
    echo "options=[${options}]"
    if [[ ${options} == 0 ]]; then
        source build-sim-cdc/setup-build-env
    elif [[ ${options} == 1 ]]; then
        source build-qcom-adp/setup-build-env
    elif [[ ${options} == 2 ]]; then
        source build-qcom-cdc/setup-build-env
    else
        echo "Environment variable setting failed !!!"
        exit 1
    fi

    local log="${DOCKER_HOME_PATH}/log/bitbake_cleansstate_unit.log"
    :> ${log}
    echo "<<<<<<<<<<<<<<<<<<<<<<<< bitbake_cleansstate start <<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
    echo ""
    for repo_info in ${REPO_RECIPES[@]}; do
        local tmp_date=(${repo_info//:/ })
        local group_name=${tmp_date[1]}
        local repo_name=${tmp_date[2]}
        # local repo_path=${tmp_date[3]}
        # local repo_bb_file=${tmp_date[4]}
        # local repo_srcuri_name=${tmp_date[5]}
        # local repo_srcrev_name=${tmp_date[6]}
        echo "[INFO ] >>>>>>>>>>> bitbake -c cleansstate [${repo_name}] start " | tee -a ${log}
        bitbake -c cleansstate ${repo_name}
        echo "[INFO ] >>>>>>>>>>> bitbake -c cleansstate [${repo_name}] end  " | tee -a ${log}
        sleep 1
    done

    echo "[INFO ] >>>>>>>>>>> bitbake -c cleansstate [arene-image-cockpit-cdc] start " | tee -a ${log}
    bitbake -c cleansstate arene-image-cockpit-cdc
    echo "[INFO ] >>>>>>>>>>> bitbake -c cleansstate [arene-image-cockpit-cdc] end  " | tee -a ${log}
    echo "<<<<<<<<<<<<<<<<<<<<<<<< bitbake_cleansstate end <<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
    echo ""
    echo "The cleanup is complete. Do I need to execute [bitbake arene-image-cockpit-cdc]?:"
    echo "0 : Execute [bitbake arene-image-cockpit-cdc]"
    echo "1 : Not Executed [bitbake arene-image-cockpit-cdc]"
    read -p "Please enter your options:" options
    echo "options=[${options}]"
    if [[ ${options} == 0 ]]; then
        bitbake arene-image-cockpit-cdc
    fi
    echo "<<<<<<<<<<<<<<<<<<<<<<<< bitbake arene-image-cockpit-cdc end <<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
}

function check_remote_repository ()
{
    if [[ $(pwd) == ${HOME_PATH}/tool ]]; then
        if [[ ! -e ${HOME_GITLAB_PATH} ]]; then
            mkdir -p ${HOME_GITLAB_PATH}
        fi
    else
        echo "The current execution directory is wrong !!!"
        exit 1
    fi
	local log="${HOME_PATH}/log/${SP}_check_remote_repository.log"
	:> ${log}

    pushd ${HOME_GITLAB_PATH}
    {
        for repo_info in ${REPO_RECIPES[@]}; do
            tmp_array=(${repo_info//:/ })
            branch_type=${tmp_array[0]}
            group_name=${tmp_array[1]}
            repo_name=${tmp_array[2]}
            ret_code=0

            echo ">>>>>>>>>>>>>>>> [${repo_name}] => create GITLAB branch start <<<<<<<<<<<<<<<<" | tee -a ${log}
            if git ls-remote --exit-code "${GITLAB_URL}/${group_name}/${repo_name}.git" > /dev/null; then
                echo "[INFO] ${GITLAB_URL}/${group_name}/${repo_name}.git remote repository is exists" | tee -a  ${log}
            else
                echo "[ERROR] ${GITLAB_URL}/${group_name}/${repo_name}.git remote repository is not exists [ERROR] !!!" | tee -a  ${log}
            fi
            echo ">>>>>>>>>>>>>>>> [${repo_name}] => create GITLAB branch end   <<<<<<<<<<<<<<<<" | tee -a ${log}
            echo "" | tee -a ${log}
        done
    }
    popd
}

function create_release_branch ()
{
    if [[ $(pwd) == ${HOME_PATH}/tool ]]; then
        if [[ ! -e ${HOME_GITLAB_PATH} ]]; then
            mkdir -p ${HOME_GITLAB_PATH}
        fi
    else
        echo "The current execution directory is wrong !!!"
        exit 1
    fi
	local log="${HOME_PATH}/log/${SP}_create_branch.log"
	local commit_id_file="${HOME_GITLAB_PATH}/${SP}_release_commit_id.txt"
	:> ${log}
	:> ${commit_id_file}

    pushd ${HOME_GITLAB_PATH}
    {
        for repo_info in ${REPO_RECIPES[@]}; do
            tmp_array=(${repo_info//:/ })
            branch_type=${tmp_array[0]}
            group_name=${tmp_array[1]}
            repo_name=${tmp_array[2]}
            ret_code=0

            echo ">>>>>>>>>>>>>>>> [${repo_name}] => create GITLAB branch start <<<<<<<<<<<<<<<<" | tee -a ${log}
            if git ls-remote --exit-code "${GITLAB_URL}/${group_name}/${repo_name}.git" > /dev/null; then
                echo "${GITLAB_URL}/${group_name}/${repo_name}.git remote repository is exists" | tee -a  ${log}
                # git update
                if [[ -d ${repo_name} ]] && [[ -e ${repo_name}/.git ]] ; then
                    pushd ${repo_name} >> ${log}
                    {
                        git reset --hard HEAD >/dev/null 2>&1
                        git clean -df >/dev/null 2>&1
                        git remote update origin --prune >/dev/null 2>&1
                        git checkout master  >>${log} 2>&1
                        ret_code=$?
                    }
                else
                    ## gitlab clone
                    git clone --progress "${GITLAB_URL}/${group_name}/${repo_name}.git" ${repo_name} > ${HOME_GITLAB_PATH}/.git_progress.log 2>&1
                    pushd ${repo_name} >> ${log}
                    {
                        git checkout master  >>${log} 2>&1
                        ret_code=$?
                    }
                fi
                # create branch
                if [[ ${ret_code} -eq 0 ]]; then
                    git pull  >>${log} 2>&1
                    if [[ $? -eq 0 ]]; then
                        echo "######### ${repo_name} #########" | tee -a ${commit_id_file}
                        SRCREV="MASTER_commit_id = \"$(git rev-parse HEAD)\""
                        echo ${SRCREV}  >> ${commit_id_file}
                        if [[ ${branch_type} == "code" ]]; then
                            echo ">>>>>>>> delete branch = ${GITLAB_YOCTO_BRANCH} START" | tee -a ${log}
                            git push origin :${GITLAB_YOCTO_BRANCH}  >>${log} 2>&1
                            git branch -D ${GITLAB_YOCTO_BRANCH}  >>${log} 2>&1
                            echo ">>>>>>>> delete branch = ${GITLAB_YOCTO_BRANCH} END" | tee -a ${log}
                            echo ""  >> ${log}
                            echo ">>>>>>>> create branch = ${GITLAB_YOCTO_BRANCH} START" | tee -a  ${log}
                            git checkout -b ${GITLAB_YOCTO_BRANCH}  >>${log} 2>&1
                            git push -f origin ${GITLAB_YOCTO_BRANCH}  >>${log} 2>&1
                            SRCREV="${GITLAB_YOCTO_BRANCH}_commit_id = \"$(git rev-parse HEAD)\""
                            echo ${SRCREV}  >> ${commit_id_file}
                            git checkout master  >>${log} 2>&1
                            git branch -D ${GITLAB_YOCTO_BRANCH}  >>${log} 2>&1
                            echo ">>>>>>>> create branch = ${GITLAB_YOCTO_BRANCH} END" | tee -a ${log}
                            echo ""  >> ${log}
                        else
                            echo ">>>>>>>> delete branch = ${GITLAB_MKDOCS_BRANCH} START" | tee -a ${log}
                            git push origin :${GITLAB_MKDOCS_BRANCH}  >>${log} 2>&1
                            git branch -D ${GITLAB_MKDOCS_BRANCH}  >>${log} 2>&1
                            echo ">>>>>>>> delete branch = ${GITLAB_MKDOCS_BRANCH} END" | tee -a ${log}
                            echo ""  >> ${log}
                            echo ">>>>>>>> create branch = ${GITLAB_MKDOCS_BRANCH} START" | tee -a ${log}
                            git checkout -b ${GITLAB_MKDOCS_BRANCH}  >>${log} 2>&1
                            git push -f origin ${GITLAB_MKDOCS_BRANCH}  >>${log} 2>&1
                            SRCREV="${GITLAB_MKDOCS_BRANCH}_commit_id = \"$(git rev-parse HEAD)\""
                            echo ${SRCREV}  >> ${commit_id_file}
                            git checkout master  >>${log} 2>&1
                            git branch -D ${GITLAB_MKDOCS_BRANCH}  >>${log} 2>&1
                            echo ">>>>>>>> create branch = ${GITLAB_MKDOCS_BRANCH} END" | tee -a ${log}
                            echo ""  >> ${log}
                        fi
                        if [[ ${branch_type} == "draft" ]]; then
                            echo ">>>>>>>> delete branch = ${GITLAB_DRAFT_BRANCH} START" | tee -a ${log}
                            git push origin :${GITLAB_DRAFT_BRANCH}  >>${log} 2>&1
                            git branch -D ${GITLAB_DRAFT_BRANCH}  >>${log} 2>&1
                            echo ">>>>>>>> delete branch = ${GITLAB_DRAFT_BRANCH} END" | tee -a ${log}
                            echo ">>>>>>>> create branch = ${GITLAB_DRAFT_BRANCH} START" | tee -a ${log}
                            git checkout draft  >>${log} 2>&1
                            git pull  >>${log} 2>&1
                            SRCREV="draft_commit_id = \"$(git rev-parse HEAD)\""
                            echo ${SRCREV}  >> ${commit_id_file}
                            git checkout -b ${GITLAB_DRAFT_BRANCH}  >>${log} 2>&1
                            git push -f origin ${GITLAB_DRAFT_BRANCH}  >>${log} 2>&1
                            SRCREV="${GITLAB_DRAFT_BRANCH}_commit_id = \"$(git rev-parse HEAD)\""
                            echo ${SRCREV}  >> ${commit_id_file}
                            git checkout master  >>${log} 2>&1
                            git branch -D ${GITLAB_DRAFT_BRANCH}  >>${log} 2>&1
                            echo ">>>>>>>> create branch = ${GITLAB_DRAFT_BRANCH} END" | tee -a ${log}
                        fi
                        echo "" >> ${commit_id_file}
                    fi
                else
                    echo ">>>>>>>>>>>>>>>> ${repo_name} git update [ERROR] !!! <<<<<<<<<<<<<<<<" | tee -a  ${log}
                    echo "######### ${repo_name} #########" | tee -a ${commit_id_file}
                    echo "######### ${repo_name} git update [ERROR] !!! #########" | tee -a  ${commit_id_file}
                    echo "" >> ${commit_id_file}
                fi
                popd >> ${log}
            else
                echo ">>>>>>>>>>>>>>>> ${GITLAB_URL}/${group_name}/${repo_name}.git remote repository is not exists [ERROR] !!! <<<<<<<<<<<<<<<<" | tee -a  ${log}
                echo "######### ${GITLAB_URL}/${group_name}/${repo_name}.git remote repository is not exists [ERROR] !!! #########" | tee -a  ${commit_id_file}
            fi
            echo ">>>>>>>>>>>>>>>> [${repo_name}] => create GITLAB branch end <<<<<<<<<<<<<<<<" | tee -a ${log}
            echo "" >> ${log}
            echo "" >> ${log}
        done
    }
    popd
}

######################################################
# main
######################################################
while :
do
    echo "#####################################################################"
    echo "#         Please select one of the options below to proceed         #"
    echo "# Options:                                                          #"
    echo "#         1   check yocto branch (linux)                            #"
    echo "#         2   create yocto branch and commitid file (linux)         #"
    echo "#         3   update bb file txt (docker)                           #"
    echo "#         4   bitbake cleansstate unit and cdc (docker)             #"
    echo "#         5   Yocto Build Result backup and result_check (docker)   #"
    echo "#         6   revert bb file txt (docker)                           #"
    echo "#         0   exit                                                  #"
    echo "#####################################################################"

    read -p "Please enter your options:" options
    echo ""
    
    case ${options} in
            1)
                start_run
                check_remote_repository
                succ_exit
                ;;
            2)
                start_run
                create_release_branch
                succ_exit
                ;;
            3)
                start_run
                update_bb_file
                succ_exit
                ;;
            4)
                start_run
                bitbake_cleansstate
                succ_exit
                ;;
            5)
                start_run
                runtime_docker_backup
                result_check
                # revert_bb_file
                succ_exit
                ;;
            6)
                start_run
                revert_bb_file
                succ_exit
                ;;
            0)
                echo "[NORMAL] -- exit 0 --"
                exit 0
                ;;
            *)
                echo "[ERROR ] ------ wrong option = [ ${options} ]!!! ------"
                ;;
    esac
done
